package com.zhongzhi.javafx.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Properties;
import java.util.ResourceBundle;
import java.util.Set;

import org.apache.ibatis.javassist.CtField.Initializer;
import org.apache.log4j.Logger;
import org.omg.PortableInterceptor.INACTIVE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.zhongzhi.ZhongzhiExcavatorSinking5Application;
import com.zhongzhi.bean.Configuration;
import com.zhongzhi.common.Common;
import com.zhongzhi.common.Plc;
import com.zhongzhi.common.Primary;
import com.zhongzhi.javafx.View.TypeView;
import com.zhongzhi.service.TypeService;
import com.zhongzhi.serviceimpl.EquipmentServiceImpl;
import com.zhongzhi.springboot.pojo.Computers;
import com.zhongzhi.springboot.pojo.Equipment;
import com.zhongzhi.springboot.pojo.EquipmentShow;
import com.zhongzhi.springboot.pojo.Station;
import com.zhongzhi.springboot.pojo.StationShow;
import com.zhongzhi.thread.PageThread;
import com.zhongzhi.thread.PlcGuardThread;
import com.zhongzhi.thread.ServerTherad;
import com.zhongzhi.thread.TaskGuardTherad;
import com.zhongzhi.util.ClientUtil;
import com.zhongzhi.util.ConfigUtil;
import com.zhongzhi.util.JsonResult;
import com.zhongzhi.util.PageUtil;

import de.felixroske.jfxsupport.FXMLController;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.MenuBar;

import javafx.scene.control.MenuItem;

import javafx.scene.control.ScrollPane;
import javafx.scene.control.TabPane;
import javafx.scene.image.Image;
import javafx.stage.Modality;
import javafx.stage.Stage;
import lombok.extern.log4j.Log4j;

@Log4j
@FXMLController
public class MainController implements Initializable{
	@FXML
	private MenuBar Menubar;
	@FXML
	private MenuItem monitoring;
	@FXML
	private MenuItem equipment;
	@FXML
	private MenuItem aisle;
	@FXML
	private MenuItem type;
	@FXML
	private MenuItem history;
	@FXML
	private MenuItem specification;
	@FXML
	private MenuItem relation;
	@FXML
	private MenuItem computers;
	@FXML
	private ScrollPane sp;
	@FXML
	private MenuItem parameterPage;
	
	@Override
	public void initialize(URL location, ResourceBundle resources) {

		log.info("主画面:开始初始化");
		init();			//初始化
		init2();		//按钮初始化
		log.info("主画面:开始初始化完成");
	}
	private void init2() {
		// TODO Auto-generated method stub
		//参数设置画面
		parameterPage.setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent event) {//打开画面代码
				Parent root;
				try {
					root = FXMLLoader.load(getClass().getResource("/fxml/run/parameter.fxml"));
					Scene scene = new Scene(root);
					Stage stage = new Stage();
					stage.setScene(scene);
					stage.setTitle("修改参数");
					stage.getIcons().add(new Image("/Images/tb.png"));
					stage.setResizable(false);//窗口不可改变大小
					stage.initModality(Modality.APPLICATION_MODAL);//窗口模态
					stage.show();//窗口出现
					
				} catch (IOException e) {
					e.printStackTrace();
				}

			}
		});
		//计算机管理
		computers.setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent event) {
				try {
					sp.setContent(FXMLLoader.load(getClass().getResource("/fxml/run/computers.fxml")));
					
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});

		type.setOnAction(new EventHandler<ActionEvent>() {
			//增加点击机型弹出画面事件
			@Override
			public void handle(ActionEvent event) {
				try {
					sp.setContent(FXMLLoader.load(getClass().getResource("/fxml/run/Type.fxml")));
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		});
		//增加点击机工位出画面事件
		aisle.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				// TODO Auto-generated method stub
				try {
					sp.setContent(FXMLLoader.load(getClass().getResource("/fxml/run/Station.fxml")));
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		//设备画面
		equipment.setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent event) {
				try {
					sp.setContent(FXMLLoader.load(getClass().getResource("/fxml/run/Equipment.fxml")));
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		//运行监控画面
		monitoring.setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent event) {
				sp.setContent(Common.getTp());
			}
		});
		//历史数据画面
		history.setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent event) {
				try {
					sp.setContent(FXMLLoader.load(getClass().getResource("/fxml/run/Measure.fxml")));
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		relation.setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent event) {
				// TODO Auto-generated method stub
				Parent root;
				try {
					root = FXMLLoader.load(getClass().getResource("/fxml/New/MonUp.fxml"));
					Scene scene = new Scene(root);
					Stage stage = new Stage();
					stage.setScene(scene);
					stage.setTitle("联系我们");
					stage.getIcons().add(new Image("/Images/tb.png"));
					stage.setResizable(false);//窗口不可改变大小
					stage.initModality(Modality.APPLICATION_MODAL);//窗口模态
					stage.show();//窗口出现
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
	}
	/**
	 * 初始化程序
	 * 1:设置默认打开画面
	 * 2:将公共画面存入公共对象
	 * 3:发起http请求,获取全部的plc集合
	 * 4:启动plc守护线程
	 * 5:启动工位守护线程
	 */
	private void init() {
		
		Common.setPage(new Integer(1));			//设置打开默认页面
		Common.setSp(sp);						//把主画面节点放到公共类
		//下面所有代码,放到了下面这个类中
		PageThread pageThread = new PageThread();
		pageThread.start();
		
//		getParameters();						//获取本机参数
//		selectCommonPlc();						//获取设备公共数据
//		selectCommonPlcAsStation();				//查询本机设备对象以及设备下的工位
//		selectCommonPlcAsStation2();			//根据plc地址集合查询工位信息
		
//		//启动plc监控线程
//		PlcGuardThread plcGuardThread = new PlcGuardThread();
//		plcGuardThread.start();
//		//启动工位业务线程
//		TaskGuardTherad taskGuardTherad = new TaskGuardTherad();
//		taskGuardTherad.start();
//		//打开连接服务器测试线程
//		ServerTherad therad = new ServerTherad();
//		therad.start();
		
		
		//打开跳转到监控画面
//		try {
//			
//			TabPane load = FXMLLoader.load(getClass().getResource("/fxml/run/TABS.fxml"));
//			Common.setTp(load);
//			sp.setContent(load);
//		} catch (IOException e1) {
//			log.error("主画面:跳转到监控画面失败:"+e1);
//		}
		
		
	}



	
	
//	private void getParameters() {
//		//获取本机参数
//		ConfigUtil.getConfiguration();
//	}
//	private void selectCommonPlc() {
//		/**
//		 * 向公共对象写入全部的plc信息
//		 */
//		//发起http请求,获取plc集合
//
//		try {
//			String s = "http://127.0.0.1:8080/equipment/select?pages=1&line=255";
//			log.info("主画面:发起http请求,获取plc集合:"+s);
//			String gethttp = ClientUtil.Gethttp(s);
//			log.info("主画面:发起http请求,获取plc集合结果:"+gethttp);
//			JsonResult jsonobj = ClientUtil.Jsonobj(gethttp, JsonResult.class);
//			ArrayList<EquipmentShow> eqlist = (ArrayList<EquipmentShow>) ClientUtil.Jsonlist(jsonobj.getData().toString(), EquipmentShow.class);
//			Common.setEquipmentList(eqlist);
//		} catch (Exception e) {
//			log.error("主画面:发起http请求,获取plc集合失败:"+e);
//		}
//	}
//	private void selectCommonPlcAsStation() {
//		//读取所有计算机信息
//		try {
//			String s = "http://127.0.0.1:8080/computers/select?pages=1&line=200";
//			log.info("主画面:发起http请求,获取计算机集合:"+s);
//			String stationhttp = ClientUtil.Gethttp(s);
//			log.info("主画面:发起http请求,获取计算机集合:"+stationhttp);
//			JsonResult jsonobj = ClientUtil.Jsonobj(stationhttp, JsonResult.class);
//			String string = jsonobj.getData().toString();
//			List<Computers> jsonlist = ClientUtil.Jsonlist(string, Computers.class);
//			Common.getArrayList().addAll(jsonlist);
//
//		} catch (Exception e) {
//			log.error("主画面:发起http请求,获取计算机信息失败:"+e);
//		}
//
//
//
//		/*
//		 * 向公共对象写入本机的plc信息
//		 */
//		try {
//			//1:根据本机站号动态生成查询语句
//			ArrayList<Computers>  list = Common.getArrayList();
//			if (list!=null && list.size()!=0) {
//				String property = Common.getConfiguration().getProperty();
//				String string = "http://127.0.0.1:8080/equipment/selectByIds?";
//				int a = 0;
//				for (Computers computers : list) {
//					boolean equals = computers.getComputers().equals(property);
//					if (equals) {
//						if (a==0) {
//							string = string + "ids=" + computers.getId();
//						}else {
//							string = string + "&ids=" + computers.getId();
//						}
//						a++;
//					}
//
//				}
//
//				//2:根据计算机id查询设备信息
//
//				log.info("主画面:获取本机管理的设备:"+string);
//				String stationhttp = ClientUtil.Gethttp(string);
//				log.info("主画面:获取本机管理的设备结果:"+stationhttp);
//				JsonResult jsonobj2 = ClientUtil.Jsonobj(stationhttp, JsonResult.class);
//				String s = jsonobj2.getData().toString();
//				System.out.println(s);
//				if (s!=null && s.equals("")!=true) {
//					List<EquipmentShow> equipmentlist = ClientUtil.Jsonlist(s, EquipmentShow.class);
//					System.out.println(equipmentlist);
//					HashMap<Integer, EquipmentShow> equipmentMap = Common.getEquipmentMap();
//					a = 0;
//					for (EquipmentShow equipmentShow : equipmentlist) {
//						//把本机管理的设备存入公共类
//						try {
//							equipmentMap.put(equipmentShow.getId(), equipmentShow);
//							String stationGet = "http://127.0.0.1:8080/station/selectByequipmentId?equipmentId="+equipmentShow.getId();
//							log.info("主画面:根据plcid获取工位信息:"+stationGet);
//							String b = ClientUtil.Gethttp(stationGet);
//							log.info("主画面:根据plcid获取工位信息结果:"+b);
//							JsonResult jsonobj3 = ClientUtil.Jsonobj(b, JsonResult.class);
//							String string3 = jsonobj3.getData().toString();
//							Plc plc = new Plc(equipmentShow);
//							if (string3!=null && string3.equals("")!=true) {
//								List<StationShow> jsonlist = ClientUtil.Jsonlist(string3, StationShow.class);
//								for (StationShow sShow : jsonlist) {
//									plc.getStTemData().put(sShow.getId(), sShow);
//								}
//							}
//							Primary.getMap().put(equipmentShow.getId(), plc);
//						} catch (Exception e) {
//							log.error("主画面:根据plcid获取工位信息失败:"+e);
//						}
//
//					}
//				}
//			}
//		} catch (Exception e) {
//			log.error("主画面:获取本机管理的设备失败"+e);
//		}
//
//	}
//	private void selectCommonPlcAsStation2() {
//		//根据plc地址集合查询工位信息
//		try {
//			String abc = "http://127.0.0.1:8080/station/selectByequipmentIds?";
//
//			HashMap<Integer, EquipmentShow> equipmentMap = Common.getEquipmentMap();
//			if (equipmentMap!=null && equipmentMap.size()!=0) {
//				int x = 0;
//				Set<Integer> keySet = equipmentMap.keySet();
//				for (Integer integer : keySet) {
//					if (x==0) {
//						abc=abc+"equipmentIds="+integer;
//					}else {
//						abc=abc+"&equipmentIds="+integer;
//					}
//					x++;
//				}
//				log.info("主画面:根据plc地址集合查询工位信息:"+abc);
//				String stationjson  = ClientUtil.Gethttp(abc);
//				log.info("主画面:根据plc地址集合查询工位信息结果:"+stationjson);
//				JsonResult jsonobj2 = ClientUtil.Jsonobj(stationjson, JsonResult.class);
//
//				String string = jsonobj2.getData().toString();
//				if (string!=null && string.equals("")!=true ) {
//					List<Station> stationList = ClientUtil.Jsonlist(string, Station.class);
//
//					for (Station station : stationList) {
//						Common.getTaskStationMap().put(station.getId(),station);
//					}
//
//				}
//
//			}
//
//
//		} catch (Exception e) {
//			log.error("主画面:根据plc地址集合查询工位信息失败:"+e);
//		}
//
//	}
//	
//	
	
}
